package com.jbm.enterapplication.loadpage.mode

import android.content.Context
import android.view.View
import com.jbm.enterapplication.InterfaceMa
import com.jbm.enterapplication.sharedprefer.SharedPreActivity
import com.jbm.enterapplication.utlisclass.HttpNet
import com.jbm.enterapplication.view.DoubleButtonDialog
import com.jbm.onehome.iface.IAppVersion
import rx.Observable
import rx.Observer

/**
 * Created by 家帮忙 on 2017/9/4.
 */
class Load_Mode_Pager : Load_Mode_Pager_In <String> {
    var sharedprefer: SharedPreActivity? = null

    override fun getVisionCode(context: Context?, onclick: InterfaceMa<String>) {
        sharedprefer = SharedPreActivity(context)
        val packages = context?.getPackageManager()
        val viname = packages?.getPackageInfo(context.getPackageName(), 0)
        val visionname = viname?.versionName
        var maxs = visionname?.substring(0, visionname.indexOf("."))
        var mins = visionname?.substring(visionname.indexOf(".") + 1, visionname.length)
        HttpNet.getInstans().obsevice(Observable.create(Observable.OnSubscribe<IAppVersion> { obVersion ->
            obVersion.onNext(HttpNet.getInstans().getOhInter().GetLastVersion("jbmsales.android", Integer.parseInt(maxs), Integer.parseInt(mins), 0, 0))
        })).subscribe(object : Observer<IAppVersion> {
            override fun onCompleted() {
            }

            override fun onError(e: Throwable?) {

            }

            override fun onNext(t: IAppVersion?) {

                if (t?.m_code == 200) {
                    if (!t.app_url.isEmpty()) {
                        var str = t.app_url;
                        val dialog = DoubleButtonDialog(context, t.errorinfo)
                        dialog.setNoText("下次更新")
                        dialog.setTetle_message("版本更新...")
                        dialog.setisVisibilitymess()
                        dialog.getokbtn().setOnClickListener(View.OnClickListener {
                                onclick.onSuccess(str, sharedprefer!!.getIsLogin())
                            dialog.dismiss()
                        })
                        dialog.getnobtn().setOnClickListener(View.OnClickListener {
                            if (t.milestone > 0) {
                                onclick.setError("2", sharedprefer!!.getIsLogin())
                            } else {
                                onclick.setError("1", sharedprefer!!.getIsLogin())
                            }
                            dialog.dismiss()
                        })

                    } else {
                        onclick.setError("1",sharedprefer!!.getIsLogin())
                    }

                } else if (t?.m_info == "Not Connect") {
                    onclick.setError("1", sharedprefer!!.getIsLogin())
                } else if (t?.m_code == 500) {
                    onclick.setError("1", sharedprefer!!.getIsLogin())
                } else {
                    onclick.setError("1", sharedprefer!!.getIsLogin())
                }
            }
        })
    }


}